﻿using System.Collections.Immutable;
using System.Text;
using CppAst;

namespace ast;

public class TsWrapperGenerator
{
    //declare function test(a:number,b:number):void;
    public static string GenerateMemberTsFunctionSignature(FunctionInfo functionInfo, ModuleInfo moduleInfo)
    {
  
        var tsFunctionName = TsTypeUtil.GetTsFunctionName(moduleInfo, functionInfo);

        var isConstructor = functionInfo.Function.IsConstructor;


        var functionInfoExportParameters = functionInfo.ExportParameters;
        var functionReturnType = functionInfo.ExportReturnType.Type;
        var builder = new StringBuilder();



        builder.Append($"{tsFunctionName}(");


        for (var index = 0; index < functionInfoExportParameters.Count; index++)
        {
            var exportParameter = functionInfoExportParameters[index];
            var exportParameterType = exportParameter.Type;
            if (index != 0 )
            {
                builder.Append(", ");
            }

            var goTypeString = TsTypeUtil.GetTsTypeString(exportParameterType);
            builder.AppendFormat("{0}:{1}", exportParameter.Name,
                goTypeString);
        }

        builder.Append(") :");
        if (!ExportConfig.FunctionReturnVoid(functionReturnType))
        {
            if (functionInfo.Function.IsNewArray)
            {
                var resultType = functionInfo.Function.ReturnType as CppPointerType;
                var goClassName = GoTypeUtil.GetGoClassName(resultType.ElementType as CppClass);
                builder.Append($"bigint");
                builder.Append(" ");
            }
            else if (functionInfo.Function.IsIndexArray)
            {
                builder.Append($"bigint");
                builder.Append(" ");
            }
            else
            {
                var goTypeString = TsTypeUtil.GetTsTypeString(functionReturnType, isConstructor);
                builder.Append(goTypeString);
                builder.Append(" ");
            }
        }
        else
        {
            builder.Append("void");
        }

        builder.Append(";\n");
        return builder.ToString();
    }
}